package de.bensoft.jrail.client.common;

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Inject;

import org.apache.log4j.Logger;

public class Utils {
	
	private static final Logger LOG = Logger.getLogger(Utils.class);
	
	@Inject 
	BeanManager beanManager;
	
	public Object injectClass(Class<?> clazz) {
		
		CreationalContext<?> ctx = beanManager.createCreationalContext(null);
		List<Bean<?>> lst 		 = new ArrayList<Bean<?>>(beanManager.getBeans(clazz, new AnnotationLiteral<Any>() { }));
		
		if(lst.size() == 0) {
			LOG.error("Cannot create FXForm. There is no enabled bean for class " + clazz.getName());
			return null;
		}		
		Bean<?> b = lst.get(0);		
		return beanManager.getReference(b, clazz, ctx);
		
	}
}
